#include "MyPlayer.h"
#include "ui_MyPlayer.h"
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QUrl>
#include <QMimeData>
#include <QDebug>
#include <string>
#include <algorithm>
#include <QAction>
#include <QPoint>
#include "Macro.h"



MyPlayer::MyPlayer(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MyPlayer)
{
    ui->setupUi(this);

    // label-display: enable drops, install filter
    ui->labelShow->installEventFilter(this);
    ui->labelShow->setAcceptDrops(true);

    // list-file: enable drops, install filter
    ui->listFile->installEventFilter(this);
    ui->listFile->setAcceptDrops(true);
    // enable custom menu
    //ui->listFile->setProperty("contextMenuPolicy", Qt::CustomContextMenu);
    ui->listFile->setContextMenuPolicy(Qt::CustomContextMenu);

    init_signal();

}

MyPlayer::~MyPlayer()
{
    delete ui;
}

/*-------------------------------Slot Begin-------------------------------*/
void MyPlayer::init_signal()
{
    // slot: it doesn't need parameter in connect
    // button clicked
    connect(ui->btnPre, SIGNAL(clicked()), this, SLOT(on_btnPre_clicked()));
    connect(ui->btnPlay, SIGNAL(clicked()), this, SLOT(on_btnPlay_clicked()));
    connect(ui->btnNext, SIGNAL(clicked()), this, SLOT(on_btnNext_clicked()));
    connect(ui->btnStop, SIGNAL(clicked()), this, SLOT(on_btnStop_clicked()));
    // list files
    connect(ui->listFile, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(on_listFile_DBClick_item(QListWidgetItem*)));
    connect(ui->listFile, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(onCustomContextMenuRequested(const QPoint&)));

}

void MyPlayer::on_btnPre_clicked()
{
    qDebug() << "pre click\n";
}

void MyPlayer::on_btnPlay_clicked()
{
    qDebug() << "play click\n";
}

void MyPlayer::on_btnNext_clicked()
{
    qDebug() << "next click\n";
}

void MyPlayer::on_btnStop_clicked()
{
    qDebug() << "stop click\n";
}

void MyPlayer::on_listFile_DBClick_item(QListWidgetItem *pItem)
{
    qDebug() << "Double click on item\n";
    auto nIndex = ui->listFile->currentIndex();
    auto nRow = ui->listFile->currentRow();
    qDebug() << pItem->text() << "\n";
    qDebug() << "nIndex: " <<nIndex<<", Row: " << nRow << "\n";
    qDebug() << "Double click\n";
}

void MyPlayer::onCustomContextMenuRequested(const QPoint &ptMouse)
{
    QListWidgetItem *item = ui->listFile->itemAt(ptMouse);
    if(item == nullptr) return;

    /*---------------menu------------------*/
    QMenu* pMenuFile = new QMenu;
    // delete menu
    QAction* pDelete = new QAction(tr("Delete"), this);
    pMenuFile->addAction(pDelete);
    connect(pDelete, SIGNAL(triggered()), this, SLOT(on_listFile_menu_delete()));
    // open menu
     QAction* pOpen = new QAction(tr("Open"), this);
    pMenuFile->addAction(pOpen);
    connect(pOpen, SIGNAL(triggered()), this, SLOT(on_listFile_menu_delete()));

    pMenuFile->exec(QCursor::pos());

    FREE_POINTER(pDelete);
    FREE_POINTER(pOpen);
    FREE_POINTER(pMenuFile);
}

void MyPlayer::on_listFile_menu_delete()
{
    // 带参数触发的信号暂时有问题
    qDebug() << "Delete menu clicked\n";
}

void MyPlayer::on_listFile_menu_open()
{
    qDebug() << "Open menu clicked\n";
}

/*-------------------------------Slot End-------------------------------*/

bool MyPlayer::eventFilter(QObject *pObject, QEvent *pEvent)
{
    // catch drag signal for label-show and list-file
    if (pObject == ui->labelShow || pObject == ui->listFile)
    {
        switch(pEvent->type())
        {
        case QEvent::DragEnter:
        {
            // [[2]]: 当拖放时鼠标进入label时, label接受拖放的动作
            QDragEnterEvent* pDragEnterEvt = dynamic_cast<QDragEnterEvent*>(pEvent);
            pDragEnterEvt->acceptProposedAction();
            return true;
        }
        case QEvent::Drop:
        {
            // [[3]]: 当放操作发生后, 取得拖放的数据
            QDropEvent* pDropEvt = dynamic_cast<QDropEvent*>(pEvent);
            QList<QUrl> urls = pDropEvt->mimeData()->urls();

            if (urls.isEmpty()) return true;
            FileInfo infoFile;
            infoFile.qstrFullPath = urls.first().toLocalFile();
            qDebug() << "File path: " << infoFile.qstrFullPath << "\n";
            // [[4]]: 在label上显示拖放的图片
            /*
            QImage image(path); // QImage对I/O优化过, QPixmap对显示优化
            if (!image.isNull()) {
                image = image.scaled(ui->label->size(),
                                     Qt::KeepAspectRatio,
                                     Qt::SmoothTransformation);
                ui->label->setPixmap(QPixmap::fromImage(image));
            }
            */
            // file not exists
            if(!check_file_exist(infoFile) && infoFile.ParseFromPath())
            {
                qDebug() << "File path: "<< infoFile.qstrFilepath<< ", File name: " << infoFile.qstrFilename  << "\n";
                ui->listFile->addItem(infoFile.qstrFilename);
                m_vecFile.push_back(infoFile);
            }
            return true;
        }
        default:
            break;
        }

    }
    return QMainWindow::eventFilter(pObject, pEvent);
}

/**
 * @brief:    check file exist
 * @para[in]
 *
 */
bool MyPlayer::check_file_exist(const FileInfo& infoFile)
{
    for(const auto& item : m_vecFile)
    {
        if(0 == item.qstrFullPath.compare(infoFile.qstrFullPath))
        {
            return true;
        }
    }
    return false;
}








